package com.fingard.dsp.bank.directbank.zyb02;

import java.math.BigDecimal;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IExchangeRate;
import com.fingard.dsp.bank.reqrespobj.exchangerate.ReqExchangeRateQry;
import com.fingard.dsp.bank.reqrespobj.exchangerate.RetAtsExchangeRateQry;
import com.fingard.dsp.bank.reqrespobj.exchangerate.detail.RetAtsExchangeRateDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ZYB02ExchangeRate extends ZYB02Base implements IExchangeRate {
    @Override
    public RetAtsExchangeRateQry exchangeRate(ReqExchangeRateQry p_reqrate) throws Exception {
        String seqNumberStart = getBankConfig().getMapValueByKey("Sys_Head","CONSUMER_ID")+reqHeader.corporation;
        RetAtsExchangeRateQry retAts = new RetAtsExchangeRateQry(p_reqrate.reqHeader);
        RetAtsExchangeRateDetail tmpRetDetail = new RetAtsExchangeRateDetail(p_reqrate.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            while (p_reqrate.readRecord()) {
                //赋值通用参数
                ZYB02Header bankHeader = new ZYB02Header();
                bankHeader.SEQ_NO = seqNumberStart+reqHeader.transSeqID;
                bankHeader.TRAN_CODE = getBankConfig().getMapValueByKey("TRANS_CODE", "EXCHANGERATE");
                String transTime = p_reqrate.reqHeader.transTime;
                bankHeader.TRAN_DATE = Format.Date8Format.format(DateHelper.convertToDateExtra(transTime));
                bankHeader.TRAN_TIMESTAMP = Format.Time9Format.format(DateHelper.convertToDateExtra(transTime));
                bankHeader.USER_IP = getIpInfoMap(p_reqrate.getOriReqXml()).get("userIP");
                bankHeader.SERVER_IP = getIpInfoMap(p_reqrate.getOriReqXml()).get("serverIP");
                //发银行报文的XML对象
                XmlTextWriter xmlWriter = new XmlTextWriter();
                xmlWriter.autoFormat = false;
                //写入通用的头部节点
                writeCommonHeaderExchangeRate(xmlWriter, bankHeader);

                //xmlWriter.writeElementString("COMEDATE", Format.Date8Format.format(new Date()));
                xmlWriter.writeElementString("COMEDATE", "");
                //xmlWriter.writeElementString("CUR", convertCurToBank(p_reqrate.readedDetail.sourceCur));
                xmlWriter.writeElementString("CUR", convertCurToBank(p_reqrate.readedDetail.targetCur));

                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();

                /**
                 * 国结系统的IP和端口
                 * OverSeaTcpTransport.host  [10.100.202.161]
                 * OverSeaTcpTransport.port  [9003]
                 */
                String[] tmpStrRet = sendToBankForExchangeRate(xmlWriter.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                    if (tmpBkReader != null) {
                        String tmpStatus = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/RET_STATUS");
                        String ret_code = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/RET_ARRAY/Row/RET_CODE");
                        String tmpStatusText = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/RET_ARRAY/Row/RET_MSG");
                       // String rateDate = tmpBkReader.getFirstText("/Reply_Msg/Body/COMEDATE").
                         //       replace("-","").substring(0,8);
                        if (tmpStatus != null && tmpStatus.equals(TMPSTATUS)) {
                            if (ret_code != null && ret_code.equals(RET_CODE)) {
                                String rateDate = tmpBkReader.getFirstText("/Reply_Msg/Body/COMEDATE").substring(0,10);
                                while (tmpBkReader.read("/Reply_Msg/Body/LISTRATE/Row")) {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.respInfo = tmpStatusText;
                                    tmpRetDetail.buyrate = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("HBUYPRICE")).divide(new BigDecimal(100)));
                                    tmpRetDetail.sellrate = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("HSELLPRICE")).divide(new BigDecimal(100)));
                                    tmpRetDetail.buynote = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("CBUYPRICE")).divide(new BigDecimal(100)));
                                    tmpRetDetail.sellnote = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("CSELLPRICE")).divide(new BigDecimal(100)));
                                    tmpRetDetail.rate = String.valueOf(new BigDecimal(tmpBkReader.readedXElement.getSingleText("BASEPRICE")).divide(new BigDecimal(100)));
                                    tmpRetDetail.rateDate = rateDate;
                                    tmpRetDetail.cur = tmpBkReader.readedXElement.getSingleText("CUR");
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            } else {
                                tmpRetDetail.respCode = "-1";
                                if (StringHelper.hasAnyChar(tmpStatusText)) {
                                    tmpRetDetail.respInfo = tmpStatusText;
                                } else {
                                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                                }
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpSbMsg.toString();
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            }
        } catch (Exception e) {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = LogHelper.getStackTrace(e);
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}
